package com.sg.service.base.framework.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.framework.req.QueryTargetDetailsListReq;
import com.sg.entity.DutyTargetDetail;
import com.sg.service.base.framework.MDutyTargetDetailService;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.dal.mapper.mbg.DutyTargetDetailMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MDutyTargetDetailServiceImpl
        extends ServiceImpl<DutyTargetDetailMapper, DutyTargetDetail>
        implements MDutyTargetDetailService {

    @Autowired
    DutyTargetDetailMapper dutyTargetDetailMapper;

    /**
     * 1-3-10新增目标职责明细[177]
     * gen by moon at 10/16/2022, 11:07:57 AM
     */
    @Trace(operationName = "1-3-10新增目标职责明细")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addTargetDetails(DutyTargetDetail dutyTargetDetail) {
        dutyTargetDetail.setDutyTargetDetailId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        dutyTargetDetail.setIsArchive("FALSE");
        dutyTargetDetail.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        dutyTargetDetail.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (dutyTargetDetail != null && dutyTargetDetail.getSubjectLifeCycle() != null && dutyTargetDetail.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            dutyTargetDetail.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        dutyTargetDetail.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        dutyTargetDetail.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (dutyTargetDetail != null && dutyTargetDetail.getSubjectLifeCycle() != null && dutyTargetDetail.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            dutyTargetDetail.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        dutyTargetDetail.setIsValid("FALSE");
        dutyTargetDetail.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        dutyTargetDetail.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(dutyTargetDetail)) {
            return dutyTargetDetail.getDutyTargetDetailId();
        } else {
            return "-1";
        }

    }

    /**
     * 1-3-10修改目标职责明细[179]
     * gen by moon at 10/16/2022, 11:07:58 AM
     */
    @Trace(operationName = "1-3-10修改目标职责明细")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateTargetDetail(DutyTargetDetail dutyTargetDetail) {
        dutyTargetDetail.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (dutyTargetDetail != null && dutyTargetDetail.getSubjectLifeCycle() != null && dutyTargetDetail.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            dutyTargetDetail.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (dutyTargetDetail != null && dutyTargetDetail.getSubjectLifeCycle() != null && dutyTargetDetail.getSubjectLifeCycle().equals("ARCHIVING")) {
            dutyTargetDetail.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        dutyTargetDetail.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (dutyTargetDetail != null && dutyTargetDetail.getSubjectLifeCycle() != null && dutyTargetDetail.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            dutyTargetDetail.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (dutyTargetDetail != null && dutyTargetDetail.getSubjectLifeCycle() != null && dutyTargetDetail.getSubjectLifeCycle().equals("ARCHIVING")) {
            dutyTargetDetail.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(dutyTargetDetail);
    }

    /**
     * 1-3-10查询目标职责明细列表[181]
     * gen by moon at 10/16/2022, 11:07:59 AM
     */
    @Trace(operationName = "1-3-10查询目标职责明细列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<DutyTargetDetail> queryTargetDetailsList(QueryTargetDetailsListReq reqDto) {

        try {
            QueryWrapper<DutyTargetDetail> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(DutyTargetDetail.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(DutyTargetDetail.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(DutyTargetDetail.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
            queryWrapper.orderByDesc("operate_time");
            return ((List<DutyTargetDetail>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-3-10批量修改目标职责明细[933]
     * gen by moon at 10/16/2022, 11:07:59 AM
     */
    @Trace(operationName = "1-3-10批量修改目标职责明细")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateTargetDetail(List<DutyTargetDetail> dutyTargetDetail) {

        if (dutyTargetDetail == null || dutyTargetDetail.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(dutyTargetDetail);

    }

    /**
     * 1-3-10批量删除目标职责明细[942]
     * gen by moon at 10/16/2022, 11:08:00 AM
     */
    @Trace(operationName = "1-3-10批量删除目标职责明细")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteTargetDetail(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * M1查询目标职责明细测试列表[4901]
     * gen by moon at 10/16/2022, 11:08:00 AM
     */
    @Trace(operationName = "M1查询目标职责明细测试列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<DutyTargetDetail> queryDutyTargetDetailTestDataList(DutyTargetDetail dutyTargetDetail) {

        try {
            QueryWrapper<DutyTargetDetail> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(dutyTargetDetail);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(DutyTargetDetail.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(DutyTargetDetail.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(DutyTargetDetail.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<DutyTargetDetail>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * M1批量删目标职责明细测试[4902]
     * gen by moon at 10/16/2022, 11:08:01 AM
     */
    @Trace(operationName = "M1批量删目标职责明细测试")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteDutyTargetDetailTestData(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

}

